package org.jeecg.modules.quartz.job;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.fund.entity.Fund;
import org.jeecg.modules.fund.entity.UserhomeOrder;
import org.jeecg.modules.fund.mapper.FundMapper;
import org.jeecg.modules.fund.mapper.UserhomeOrderMapper;
import org.jeecg.modules.system.mapper.SysUserMapper;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 示例不带参定时任务
 * 
 * @Author Scott
 */
@Slf4j
public class OrderJob implements Job {

	@Resource
	UserhomeOrderMapper userhomeOrderMapper;

	@Resource
	FundMapper fundMapper;

	@Override
	public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
		log.info(" Job Execution key："+jobExecutionContext.getJobDetail().getKey());
		LambdaQueryWrapper<UserhomeOrder> userhomeOrderLambdaQueryWrapper=new LambdaQueryWrapper<>();
		userhomeOrderLambdaQueryWrapper.eq(UserhomeOrder::getStatus,0);
		userhomeOrderLambdaQueryWrapper.eq(UserhomeOrder::getType,1);
		// 获取当前时间
		Calendar calendar = Calendar.getInstance();
		// 将当前时间的分钟减一
		calendar.add(Calendar.MINUTE, -30);
		// 获取调整后的时间
		Date adjustedDate = calendar.getTime();
		userhomeOrderLambdaQueryWrapper.lt(UserhomeOrder::getCreateTime,adjustedDate);
		List<UserhomeOrder> userhomeOrderList=userhomeOrderMapper.selectList(userhomeOrderLambdaQueryWrapper);
		for (UserhomeOrder temp:userhomeOrderList)
		{
			temp.setStatus(2);
			userhomeOrderMapper.updateById(temp);
			//处理基金库存
			Fund fund=fundMapper.selectById(temp.getFundId());
			fund.setStockNum(fund.getStockNum()+temp.getNum());
			fund.setBuyNum(fund.getBuyNum() - temp.getNum());
			fundMapper.updateById(fund);
		}
		log.info(String.format(" Jeecg-Boot 普通定时任务 OrderJob !  时间:" + DateUtils.getTimestamp()));
	}
}
